/**
 * @var AJAX
 */

layui.define(['jquery', 'layer'], function (exports) {
    var $ = layui.jquery
        , layer = layui.layer
        , Class = function (url, success, error, ant) {
        this.ant = ant ? ant : true;
        this.antIndex = 0;
        this.options.url = url;

        this.setResult(success, error)
        this.setBeforeSend();
        this.setError();
    }
        , AJAX = function (url, success, error) {
        return new Class(url, success, error)
    };

    Class.prototype.data = {}; //创建json对象
    
    Class.prototype.setResult = function (success, error) {

        if (success instanceof Function)
            this.success = success;

        if (error instanceof Function)
            this.error = error;

        var that = this;

        this.options.success = function (data) {
            if (that.ant === true) layer.close(that.antIndex);
            var code = data.code;
            switch (code) {
                case 302:
                case 403:
                    window.location.href = data.redirect;
                    break;
                case 0:
                    that.success(data);
                    break;
                default:
                    that.error(data);
            }
        };
    };

    Class.prototype.setBeforeSend = function (callback) {
        var that = this;
        this.options.beforeSend = function (xhr) {
            // xhr.setRequestHeader('AOP', 1);
            if (that.ant === true) {
                that.antIndex = layer.load();
            }
            if (callback instanceof Function) callback(xhr)
        }
    };

    Class.prototype.setError = function (callback) {
        var that = this;
        this.options.error = function (XMLHttpRequest, textStatus, errorThrown) {
            if (that.ant === true) {
                layer.close(that.antIndex);
            }
            if (callback instanceof Function)
                callback(XMLHttpRequest, textStatus, errorThrown)
            else layer.msg(XMLHttpRequest.responseText)
        };
    };

    Class.prototype.success = function (res) {
        layer.msg('操作成功')
    };

    Class.prototype.error = function (res) {
        layer.msg(res.msg, {icon: 5, anim: 6})
    };

    Class.prototype.setData = function (obj) {
        this.data = obj
    };

    Class.prototype.setDataValue = function (key, value) {
        this.data[key] = value
    };

    Class.prototype.setDataCondition = function (obj) {
        this.setDataValue('conditions', obj)
    }

    Class.prototype.setDataConditionValue = function (key, value) {
        var conditions = this.data.hasOwnProperty('conditions') ? this.data.conditions : {}
        conditions[key] = value;
        this.setDataValue('conditions', conditions)
    }

    Class.prototype.setDataData = function (obj) {
        this.setDataValue('data', obj)
    }

    Class.prototype.setDataDataValue = function (key, value) {
        var data = this.data.hasOwnProperty('data') ? this.data.data : {}
        data[key] = value;
        this.setDataValue('data', data)
    }

    Class.prototype.setDataSort = function (obj) {
        this.setDataValue('sort', obj)
    }

    Class.prototype.setDataSortValue = function (key, value) {
        var sort = this.data.hasOwnProperty('sort') ? this.data.sort : {}
        sort[key] = value;
        this.setDataValue('sort', sort)
    }

    Class.prototype.setMethod = function (method) {
        this.options.type = method
    };

    Class.prototype.setHeaders = function (obj) {
        this.options.headers = obj
    }

    Class.prototype.setHeadersValue = function (key, value) {
        this.options.headers[key] = value
    }

    Class.prototype.setOption = function (key, value) {
        this.options[key] = value;
    }

    Class.prototype.getOption = function (key) {
        return this.options[key];
    }

    Class.prototype.removeOption = function (key) {
        delete this.options[key];
    }

    Class.prototype.options = {
        url: "",
        type: "get",
        beforeSend: function (xhr) {
            // xhr.setRequestHeader('AOP', 1);
        },
        headers: {},
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    }

    Class.prototype.send = function () {
        this.options.data = this.data
        $.ajax(this.options)
    }

    Class.prototype.sendJson = function () {
        this.options.data = JSON.stringify(this.data)
        this.setHeadersValue("Content-Type", "application/json")
        this.dataType = "json"
        this.options.type = "post"
        $.ajax(this.options)
    }

    exports('ax', AJAX)

})